import datetime, requests, json, calendar
from datetime import date, timedelta, datetime as diuu


def get_Salestrend():
    today = datetime.date.today().strftime('%Y%m%d')
    today2 = datetime.date.today()
    now = datetime.date.today()
    this_month_start = datetime.datetime(now.year, now.month, 1).strftime('%Y/%m/%d')
    this_month_end = datetime.datetime(now.year, now.month, calendar.monthrange(now.year, now.month)[1]).strftime(
        '%Y/%m/%d')
    last_day_of_last_month = datetime.date(today2.year, today2.month, 1) - datetime.timedelta(1)
    first_day_of_last_month = datetime.date(last_day_of_last_month.year, last_day_of_last_month.month, 1)
    loginurl = 'https://auth.xincheng.com/LoginService.ashx'
    listurl = 'http://passport.pri.xincheng.com/subsystems.do'
    logindata = {
        'lang': 'en - us',
        'systemCode': 'A07',
        'rememberPassword': 'false',
        'challengeNumber': '',
        'randomDate': '2021-05-23',
        '15': '34:50',
        'hidetxtPassword': 'lU/Q3OulShPF0iazGNk5O7YGzt9cMlFGiz5l/m73bCJ9WSVGCtqmnHroP6IjSat4m7HTQlMn9VGVbAssdYyR4BOsEdjB3lTY4wmzqd8gfwV5YpbaeoERAzuwpl7QzCyjEnV34Nu7eMlyXZrJLxdGLEdVxdKetMMhJlT2y5sKL1SrpPW4i5sJRC2uH2Jh2qeWkOEW87C8QF9Tyr+Hhyx8ZjH0RbtKW82gLG+edUxls2nXZ6rMkZHmOh9QrPc2/ORlIYAHItYHyd098/6+afAQavKHcmh8kGmmNW0uZYOs57ZJxwLpJ2NoDOX+YWgS57Wh6ceQXqZR3Gt7b+dDN7M1gw==',
        'txtIsTrustAccessor': 'True',
        'txtUserName': 'wangrui20',
    }
    loginheader = {
        'Referer': 'https://auth.xincheng.com/?challengeNumber=ZGY5eGpsaGt5NTB0cWRlcHJ0c2h1OXVuMDBlMHFnbXMwN2JmZGEzYmEyNTlsaHFtYXN5MHlheWRjOGNsZmIyZjF4ejJ0b3BvbW1mMHl4ZTR4b2ZlcnlrdGVsemRncG1yY3MybzVicmxyOHlqdDRsbTgzb2I5d2tpOGx3dnlvMW9zOXJ6azh4OWRoYnVkd253b2pubHNpbmN2Y3ZvOWttNjBtcW14MWV2NDFnOHpmc2s4bHN2ZmN3aGk0Y2RhZXR4c2JtbjJmYmZ6cWV1MWlibnE2OGUyOTd4azBpMGd5ODlrMmFqM2w3djMxb2d5c3N1bnFnYWFjNHV6YWluZWY5cA==&flag=3&systemCode=A07&RetutnUrl=http%3A%2F%2Fekp.xincheng.com%2Fsys%2Fportal%2Fpage.jsp',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36',
    }
    header = {
        'User-Agent': 'Mozilla/5.0(Macintosh;IntelMacOSX10_15_6) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 84.0.4147.89Safari / 537.36',
        'Referer': 'http://databi.pri.xincheng.com/',
    }
    session = requests.session()
    session.post(loginurl, data=logindata, headers=loginheader)
    tokenreq = session.get(listurl)
    tokenreq.content.decode()
    tokenreq = json.loads(tokenreq.text)
    tokenreq = tokenreq['body']['user']['orgId']
    moonthurl = 'http://databiapi.web.pri.xincheng.com/shop/global/getShopSaleInfo?token=misUser' + tokenreq + ''
    data3 = {
        'id': tokenreq,
        'from': str(this_month_start),
        'to': str(this_month_end),
        'floors': '',
        'shopTypes': '',
        'shopEntityIds': '',
        'pageIndex': 1,
        'pageSize': 500,
        'type': 0,
        'sums': 'saleDate,netSale,saleAmount,saleCount,billAvg,refundSaleAmount,refundSaleCount,claimMoneyTotal',
        'timeType': 'd',
        'orderBy': ''
    }
    data4 = {
        'id': tokenreq,
        'from': str(first_day_of_last_month),
        'to': str(last_day_of_last_month),
        'floors': '',
        'shopTypes': '',
        'shopEntityIds': '',
        'pageIndex': 1,
        'pageSize': 500,
        'type': 0,
        'sums': 'saleDate,netSale,saleAmount,saleCount,billAvg,refundSaleAmount,refundSaleCount,claimMoneyTotal',
        'timeType': 'd',
        'orderBy': ''
    }
    thismonth = []
    thatmonth = []
    thismonthdata = session.post(moonthurl, headers=header, data=data3)
    thismonthdata.content.decode()
    thismonthdata = json.loads(thismonthdata.text)
    thismonthdata = thismonthdata['data']['data']
    for i in thismonthdata:
        thismonth.append('{:.2f}'.format(i['claimMoneyTotal'] / 10000))

    thatmonthdata = session.post(moonthurl, headers=header, data=data4)
    thatmonthdata.content.decode()
    thatmonthdata = json.loads(thatmonthdata.text)
    thatmonthdata = thatmonthdata['data']['data']
    for i in thatmonthdata:
        thatmonth.append('{:.2f}'.format(i['claimMoneyTotal'] / 10000))
    return thismonth, thatmonth


def days_cur_month():  # 获取当月所有日期
    m = diuu.now().month
    y = diuu.now().year
    if m == 12:
        m = 11
        ndays = (date(y, m + 1, 1) - date(y, m, 1)).days
        d1 = date(y, 12, 1)
        d2 = date(y, 12, ndays)
        delta = d2 - d1
        return [(d1 + timedelta(days=i)).strftime('%-m月%-d日') for i in range(delta.days + 2)]
    ndays = (date(y, m + 1, 1) - date(y, m, 1)).days
    d1 = date(y, m, 1)
    d2 = date(y, m, ndays)
    delta = d2 - d1
    return [(d1 + timedelta(days=i)).strftime("%-m月%-d日") for i in range(delta.days + 1)]
